package com.example.bbsbe.service;

import com.example.bbsbe.Repository.ProfileRepository;
import com.example.bbsbe.Repository.UserRepository;
import com.example.bbsbe.entity.user;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.html.parser.Entity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@Transactional
public class UserService {
    private final UserRepository userRepository;
    private final ProfileRepository profileRepository;
    @PersistenceContext
    private EntityManager entityManager;


    public UserService(UserRepository userRepository, ProfileRepository profileRepository, EntityManager entityManager) {
        this.userRepository = userRepository;
        this.profileRepository = profileRepository;
        this.entityManager=entityManager;
    }
    public Map<String, Object> register(String name, String password, String email, String phonenum) {
        user user = new user();
        user.setName(name);
        user.setPsw(password);
        user.setEmail(email);
        user.setPhonenum(phonenum);
        user.setNickname(name);
        user.setAdmin("0");
        user.setAvatar("common.jpg");
        user.setSignature("");
        user.setLevel(0);

        userRepository.save(user);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 400);
        return response;
    }
    public Map<String, Object> login(String username, String password) {
        Optional<user> userOpt = userRepository.findByNameAndPsw(username, password);
        Map<String, Object> response = new HashMap<>();

        if (userOpt.isPresent()) {
            user user = userOpt.get();
            response.put("condition", 1);
            response.put("icon", user.getAvatar());
            response.put("nickname", user.getNickname());
            response.put("admin", user.getAdmin());
            response.put("phonenum", user.getPhonenum());
            response.put("email", user.getEmail());
            response.put("level", user.getLevel());
            response.put("signature", user.getSignature());
        } else if (userRepository.existsByName(username)) {
            response.put("condition", 2);
        } else {
            response.put("condition", 3);
        }
        return response;
    }
    public List<user> findAll() {
        List<user> users=userRepository.findAll();

        for (user entity : users){
            entityManager.detach(entity);
            Long id=entity.getId();
            String avatar=entity.getAvatar();
            entity.setUrl("https://www.yushi.chat/api/users/"+id+'/');
            entity.setAvatar("https://www.yushi.chat/media/"+avatar);
        }
;
        return users;

    }
    public Map<String, Object> getUserStats(String username) {
        user user = userRepository.findIdByName(username);
        String userId = String.valueOf(user.getId());

        // 获取已发布的资料数量
        long proCount = profileRepository.calculateTotalProfiles(userId, "1");

        // 获取总浏览数
        Integer viewsCount = profileRepository.calculateTotalViews(userId, "1");

        // 获取总评论数
        Integer discussCount = profileRepository.calculateTotalDiscuss(userId, "1");

        Map<String, Object> response = new HashMap<>();
        response.put("condition", 1);
        response.put("procount", proCount);
        response.put("viewscount", viewsCount != null ? viewsCount : 0);
        response.put("discusscount", discussCount != null ? discussCount : 0);
        return response;
    }

    public void updateUser(String username, String avatar, String nickname, String signature) {
        user user = userRepository.findByName(username);
        user.setNickname(nickname);
        user.setAvatar(avatar);
        user.setSignature(signature);
        userRepository.save(user);
    }
}
